package controller

import (
	"dc-news/common/global/consts"
	myjwt "dc-news/common/middleware/jwt"
	"dc-news/common/resp"
	"dc-news/common/utils"
	"dc-news/service"
	"dc-news/vo"
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"net/http"
)

// LoginHandler 登录
func LoginHandler(c *gin.Context) {
	req := &vo.LoginReq{}
	err := c.Bind(req)
	if err != nil {
		c.JSON(http.StatusOK, resp.NewErrResp(consts.ParameterErrorCode, "参数错误"))
		return
	}
	isEmail := utils.VerifyEmailFormat(req.Username)
	if isEmail {
		service.LoginByEmailService(c, req)
		return
	} else {
		service.LoginByUsernameService(c, req)
	}
}

// ResetPasswordEmailHandler 发送重置密码邮件
func ResetPasswordEmailHandler(c *gin.Context) {
	req := &vo.ResetPasswordEmailReq{}
	err := c.Bind(req)
	if err != nil {
		c.JSON(http.StatusOK, resp.NewErrResp(consts.ParameterErrorCode, "参数错误"))
		return
	}
	if req.Email == "" && req.Username == "" {
		c.JSON(http.StatusOK, resp.NewErrResp(consts.ParameterErrorCode, "参数错误"))
		return
	}
	service.ResetPasswordEmailService(c, req)
}

// ResetPasswordHandler 重置密码
func ResetPasswordHandler(c *gin.Context) {
	req := &vo.ResetPasswordReq{}
	err := c.Bind(req)
	if err != nil {
		c.JSON(http.StatusOK, resp.NewErrResp(consts.ParameterErrorCode, "参数错误"))
		return
	}
	service.ResetPasswordService(c, req)
}

// RegisterHandler 注册
func RegisterHandler(c *gin.Context) {
	req := &vo.RegisterReq{}
	err := c.Bind(req)
	if err != nil {
		c.JSON(http.StatusOK, resp.NewErrResp(consts.ParameterErrorCode, "参数错误"))
		return
	}
	service.RegisterService(c, req)
}

// ConfirmEmailHandler 确认邮件
func ConfirmEmailHandler(c *gin.Context) {
	req := &vo.ConfirmEmailReq{}
	err := c.Bind(req)
	if err != nil {
		c.JSON(http.StatusOK, resp.NewErrResp(consts.ParameterErrorCode, "参数错误"))
		return
	}
	service.ConfirmEmailService(c, req)
}

// GetUserInfoHandler 获取用户信息
func GetUserInfoHandler(c *gin.Context) {
	claims := c.MustGet("claims").(*myjwt.CustomClaims)
	service.GetUserInfoService(c, cast.ToInt(claims.Id))
}

// GetUserSettingHandler 获取用户设置
func GetUserSettingHandler(c *gin.Context) {
	claims := c.MustGet("claims").(*myjwt.CustomClaims)
	service.GetUserSettingService(c, cast.ToInt(claims.Id))
}

// UpdateUserSettingHandler 更新用户信息
func UpdateUserSettingHandler(c *gin.Context) {
	claims := c.MustGet("claims").(*myjwt.CustomClaims)
	req := &vo.UpdateUserSettingReq{}
	err := c.Bind(req)
	if err != nil {
		c.JSON(http.StatusOK, resp.NewErrResp(consts.ParameterErrorCode, "参数错误"))
		return
	}
	service.UpdateUserSettingService(c, cast.ToInt(claims.Id), req)
}

// UpdateUserInfoHandler 修改用户信息
func UpdateUserInfoHandler(c *gin.Context) {
	claims := c.MustGet("claims").(*myjwt.CustomClaims)
	req := &vo.UpdateUserInfoReq{}
	err := c.Bind(req)
	if err != nil {
		c.JSON(http.StatusOK, resp.NewErrResp(consts.ParameterErrorCode, "参数错误"))
		return
	}
	service.UpdateUserInfoService(c, cast.ToInt(claims.Id), req)
}

// UploadAvatarHandler 上传头像
func UploadAvatarHandler(c *gin.Context) {
	service.UploadAvatarService(c)
}

// GetUserChannelsHandler 获取用户关注频道
func GetUserChannelsHandler(c *gin.Context) {
	claims := c.MustGet("claims").(*myjwt.CustomClaims)
	service.GetUserChannelsService(c, cast.ToInt(claims.Id))
}

// GetUserFavouriteNewsHandler 获取用户收藏新闻列表
func GetUserFavouriteNewsHandler(c *gin.Context) {
	claims := c.MustGet("claims").(*myjwt.CustomClaims)
	service.GetUserFavouriteNewsService(c, cast.ToInt(claims.Id))
}

// GetUserLikesNewsHandler 获取用户收藏新闻列表
func GetUserLikesNewsHandler(c *gin.Context) {
	claims := c.MustGet("claims").(*myjwt.CustomClaims)
	service.GetUserLikesNewsService(c, cast.ToInt(claims.Id))
}

// GetUserReportService 获取用户使用报告
func GetUserReportService(c *gin.Context) {
	claims := c.MustGet("claims").(*myjwt.CustomClaims)
	service.GetUserReportService(c, cast.ToInt(claims.Id))
}
